package medium;

import java.util.HashMap;

public class Lc3484 {
    private static HashMap<String, Integer> sheet = new HashMap<>();

    public Lc3484(int rows) {
        sheet.clear();
    }

    public void setCell(String cell, int value) {
        sheet.put(cell, value);
    }

    public void resetCell(String cell) {
        if (sheet.containsKey(cell)) {
            sheet.remove(cell);
        }
    }

    public int getValue(String formula) {
        if (!formula.startsWith("=")) {
            return -1;
        }
        String[] split = formula.substring(1).split("\\+");
        if (split == null || split.length != 2) {
            return 0;
        }
        return getAnInt(split[0]) + getAnInt(split[1]);
    }

    private static int getAnInt(String splitString) {
        int val = 0;
        if (splitString.charAt(0) - '0' >= 0 && splitString.charAt(0) - '9' <= 0) {
            val = Integer.parseInt(splitString);
        } else {
            val = sheet.getOrDefault(splitString, 0);
        }
        return val;
    }
}
